package com.tarena.lbs.basic.web.repository.impl;

import com.github.pagehelper.PageInfo;

import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.mapper.StoreMapper;
import com.tarena.lbs.basic.web.repository.StoreRepository;
import com.tarena.lbs.common.basic.converter.StoreConverter;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.dos.StoreDO;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.MarketingSelectedQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StoreRepositoryImpl implements StoreRepository {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreConverter storeConverter;

    @Override
    public int shopAdd(StoreDO storeDO) {
        storeMapper.shopAdd(storeDO);
        Integer id = storeDO.getId();
        return id;
    }

    @Override
    public int deleteByID(Integer id) {
        int row = storeMapper.deleteByPrimaryKey(id);
        return row;
    }

    @Override
    public int enableStatusById(Integer id) {
        int row =storeMapper.enableStatusById(id);
        return row;
    }

    @Override
    public int disableStatusById(Integer id) {
        int row =storeMapper.disableStatusById(id);
        return row;
    }

    @Override
    public StoreDO getStoreById(Integer id) {
        StoreDO storeDO=storeMapper.selectByPrimaryKey(id);
        return storeDO;
    }

    @Override
    public List<StoreDO> selectAll(StoreQuery storeQury) {
        List<StoreDO> storeDOS = storeMapper.selectAll(storeQury);
        return storeDOS;
    }

    @Override
    public int updateByPrimaryKeySelective(StoreDO storeDO) {
        return storeMapper.updateByPrimaryKeySelective(storeDO);
    }

    @Override
    public BasePageList<StoreBO> getStoreByCity(AreaStoreQuery storeQuery) {
        int pageSize = storeQuery.getPageSize();
        int pageNum = storeQuery.getPageNo();
        //new PageQuery(pageNum, pageSize);
        List<Integer> addressIdList = new ArrayList<>();
        for (Long temp : storeQuery.getCityIdList()) {
            addressIdList.add(temp.intValue());
        }
        List<StoreDO> storeDOList = storeMapper.selectByAddressId(addressIdList);
        PageInfo<StoreDO> pageInfo = new PageInfo<>(storeDOList);
        List<StoreBO> storeBOList = pageInfo.getList().stream().map(storeDO -> {
            return storeConverter.do2bo(storeDO);
        }).collect(Collectors.toList());
        return new BasePageList<StoreBO>(storeBOList,pageInfo.getTotal());
    }

    /**
     * 功能描述: 获取选中的店铺列表
     *
     * @param storeQuery
     */
    @Override
    public BasePageList<StoreBO> marketingStoreList(MarketingSelectedQuery storeQuery) {
        int pageSize = storeQuery.getPageSize();
        int pageNum = storeQuery.getPageNo();
        //new PageQuery(pageNum, pageSize);
        List<StoreDO> storeDOList = storeMapper.selectByIdList(storeQuery.getIdList());
        PageInfo<StoreDO> pageInfo = new PageInfo<>(storeDOList);
        List<StoreBO> storeBOList = pageInfo.getList().stream().map(storeDO -> {
            return storeConverter.do2bo(storeDO);
        }).collect(Collectors.toList());
        List<Integer> areaIdList = storeDOList.stream().map(e -> e.getAreaId()).distinct().collect(Collectors.toList());
        return new BasePageList<StoreBO>(storeBOList,pageInfo.getTotal());
    }
}
